package com.bjyhl.core.web.route;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.webkit.URLUtil;
import android.webkit.WebView;

import com.bjyhl.core.fragments.LatteFragment;
import com.bjyhl.core.fragments.bottom.BaseBottomFragment;
import com.bjyhl.core.fragments.bottom.BottomItemFragment;
import com.bjyhl.core.web.WebFragment;
import com.bjyhl.core.web.WebFragmentImpl;
import com.bjyhl.core.web.WebViewFragment;

import me.yokeyword.fragmentation.SupportFragment;
import me.yokeyword.fragmentation.SupportHelper;

public class Router {

    private Router() {
    }

    //线程安全的惰性单例
    private static class Holder {
        private static final Router INSTANCE = new Router();
    }

    public static Router getInstance() {
        return Holder.INSTANCE;
    }

    public final boolean handleWebUrl(WebFragment fragment, String url) {

//        return false;

        //如果是电话协议
        if (url.contains("tel:")) {
            callPhone(fragment.getContext(), url);
            return true;
        }
//        final LatteFragment topFragment = fragment.getTopFragment();
//        final WebFragmentImpl webFragment = WebFragmentImpl.create(url);
//        topFragment.getSupportDelegate().start(webFragment);

//        // 外层跳转
//        final LatteFragment topFragment = fragment.getTopFragment();
//        final WebViewFragment secondActivity = WebViewFragment.newInstance(url);
//        topFragment.getSupportDelegate().start(secondActivity);



        // 内层跳转
        final WebViewFragment secondActivity = WebViewFragment.newInstance(url);
        fragment.getSupportDelegate().start(secondActivity);

        // 显示后退按钮
        // TODO 层次深度>n 应该已显示后退按钮了，不必处理
        Fragment pFragment = null;
        Fragment _f = fragment;
        while(true) {
            pFragment = _f.getParentFragment();
            if(pFragment instanceof BottomItemFragment || pFragment == null) {
                break;
            }
            _f = _f.getParentFragment();
        }
        if(pFragment instanceof BottomItemFragment) {
            BaseBottomFragment topFragment = (BaseBottomFragment) pFragment.getParentFragment();
            topFragment.showBackBtn(true);
        }



        return true;
    }

    private void loadWebPage(WebView webView, String url) {
        if (webView != null) {
            webView.loadUrl(url);
        } else {
            throw new NullPointerException("WebView is null !");
        }
    }

    //加载本地网页
    private void loadLocalPage(WebView webView, String url) {
        loadWebPage(webView, "file:///android_asset/" + url);
    }

    private void loadPage(WebView webView, String url) {
        if (URLUtil.isNetworkUrl(url) || URLUtil.isAssetUrl(url)) {
            loadWebPage(webView, url);
        } else {
            loadLocalPage(webView, url);
        }
    }

    public void loadPage(WebFragment fragment, String url) {
        loadPage(fragment.getWebView(), url);
    }

    private void callPhone(Context context, String uri) {
        final Intent intent = new Intent(Intent.ACTION_DIAL);
        final Uri data = Uri.parse(uri);
        intent.setData(data);
        ContextCompat.startActivity(context, intent, null);
    }
}
